package com.lian.hope.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lian.hope.mapper.VolunteerActivitiesMapper;
import com.lian.hope.mapper.VolunteerRegisterMapper;
import com.lian.hope.model.dto.VolunteerActivitiesDTO;
import com.lian.hope.model.dto.VolunteerRegisterDTO;
import com.lian.hope.model.po.VolunteerActivities;
import com.lian.hope.model.po.VolunteerRegister;
import com.lian.hope.model.vo.PageBean;
import com.lian.hope.model.vo.VolunteerActivitiesVO;
import com.lian.hope.service.VolunteerActivitiesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class VolunteerActivitiesServiceImpl implements VolunteerActivitiesService {

    private final VolunteerActivitiesMapper volunteerActivitiesMapper;
    private final VolunteerRegisterMapper volunteerRegisterMapper;

    /**
     * 分页获取志愿者活动
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<VolunteerActivitiesVO> listVolunteerActivities(Integer pageNum, Integer pageSize) {
        // 1.创建PageBean对象
        PageBean<VolunteerActivitiesVO> page = new PageBean<>();
        // 2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 3.查询mapper
        List<VolunteerActivitiesVO> list = volunteerActivitiesMapper.listVolunteerActivities();
        log.info("list:{}", list);
        Page<VolunteerActivitiesVO> projects = (Page<VolunteerActivitiesVO>)list;

        // 把数据填充到PageBean对象中
        page.setTotal(projects.getTotal());
        page.setItem(projects.getResult());
        return page;
    }

    /**
     * 随机获取五个活动
     *
     * @return
     */
    @Override
    public List<VolunteerActivitiesVO> getRandomVolunteerActivities() {
        return volunteerActivitiesMapper.getRandomVolunteerActivities();
    }

    /**
     * 新增志愿者活动
     *
     * @param volunteerActivitiesDTO
     */
    @Override
    public void insertVolunteerActivities(VolunteerActivitiesDTO volunteerActivities) {
        VolunteerActivities v = VolunteerActivities.builder().activityName(volunteerActivities.getActivityName()).brief(
                volunteerActivities.getBrief()).startTime(volunteerActivities.getStartTime()).endTime(
                volunteerActivities.getEndTime()).address(volunteerActivities.getAddress())
            .requireVolunteers(volunteerActivities.getRequireVolunteers()).createTime(LocalDateTime.now())
            .activityDescHtml(volunteerActivities.getActivityDescHtml()).contactAvatarUrl(volunteerActivities.getContactAvatarUrl()).contactPhone(volunteerActivities.getContactPhone()).contactNickname(volunteerActivities.getContactNickname()).imgUrl(volunteerActivities.getImgUrl()).build();

        volunteerActivitiesMapper.insertVolunteerActivities(v);
    }

    @Override
    public void insertVolunteerRegister(Long activityId, Long userId) {
volunteerRegisterMapper.insertVolunteerRegister(activityId,userId);
    }

    @Override
    public void deleteVolunteerRegister(Long volunteerRegister, Long userId) {
        volunteerRegisterMapper.deleteVolunteerRegister(volunteerRegister,userId);
    }

    @Override
    public VolunteerActivitiesVO getVolunteerActivitiesDetailById(Long activityId) {
       return volunteerRegisterMapper.getVolunteerActivitiesDetailById(activityId);
    }



}
